package com.authorization.project.authorization.controller;

import java.util.List;

import com.authorization.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.authorization.framework.aspectj.lang.annotation.Log;
import com.authorization.framework.aspectj.lang.enums.BusinessType;
import com.authorization.project.authorization.domain.Microservice;
import com.authorization.project.authorization.service.IMicroserviceService;
import com.authorization.framework.web.controller.BaseController;
import com.authorization.framework.web.domain.AjaxResult;
import com.authorization.common.utils.poi.ExcelUtil;
import com.authorization.framework.web.page.TableDataInfo;

/**
 * 微服务Controller
 * 
 * @author 李晓雷
 * @date 2020-04-24
 */
@RestController
@RequestMapping("/authorization/microservice")
public class MicroserviceController extends BaseController
{
    @Autowired
    private IMicroserviceService microserviceService;

    /**
     * 查询微服务列表
     */
    @PreAuthorize("@ss.hasPermi('authorization:microservice:list')")
    @GetMapping("/list")
    public TableDataInfo list(Microservice microservice)
    {
        startPage();
        List<Microservice> list = microserviceService.selectMicroserviceList(microservice);
        return getDataTable(list);
    }

    /**
     * 查询微服务集合通过产品主键（参数）
     */
    @GetMapping(value = "/productServiceList/{productId}")
    public TableDataInfo productServiceList(@PathVariable Long productId)
    {
        List<Microservice> list = microserviceService.productServiceList(productId);
        return getDataTable(list);
    }

    /**
     * 导出微服务列表
     */
    @PreAuthorize("@ss.hasPermi('authorization:microservice:export')")
    @Log(title = "微服务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Microservice microservice)
    {
        List<Microservice> list = microserviceService.selectMicroserviceList(microservice);
        ExcelUtil<Microservice> util = new ExcelUtil<Microservice>(Microservice.class);
        return util.exportExcel(list, "microservice");
    }

    /**
     * 获取微服务详细信息
     */
    @PreAuthorize("@ss.hasPermi('authorization:microservice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(microserviceService.selectMicroserviceById(id));
    }

    /**
     * 新增微服务
     */
    @PreAuthorize("@ss.hasPermi('authorization:microservice:add')")
    @Log(title = "微服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Microservice microservice)
    {
        microservice.setCreateBy(SecurityUtils.getUsername());
        return toAjax(microserviceService.insertMicroservice(microservice));
    }

    /**
     * 修改微服务
     */
    @PreAuthorize("@ss.hasPermi('authorization:microservice:edit')")
    @Log(title = "微服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Microservice microservice)
    {
        microservice.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(microserviceService.updateMicroservice(microservice));
    }

    /**
     * 删除微服务
     */
    @PreAuthorize("@ss.hasPermi('authorization:microservice:remove')")
    @Log(title = "微服务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(microserviceService.deleteMicroserviceByIds(ids));
    }

    /**
     *验证子服务中是否已经关联
     * @param microservice
     * @return
     */
    @PostMapping(value = "/verifyAssociation")
    public AjaxResult verifyAssociation(@RequestBody Microservice microservice)
    {
        return AjaxResult.success(microserviceService.verifyAssociation(microservice.getCurrentServiceId(), microservice.getNewRelationId()));
    }
}
